﻿using BrnMall.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using BrnMall.Core.Domain.Distribution;

namespace BrnMall.Data
{
	/// <summary>
	/// 分销数据访问类
	/// </summary>
	public partial class DistributionData
	{
		#region 分销参数

		/// <summary>
		/// 获取分销参数
		/// </summary>
		/// <returns></returns>
		public static DistributionParamInfo GetDistributionParam()
		{
			DistributionParamInfo info = null;
			var reader = BMAData.RDBS.GetDistributionParam();
			if (reader != null && reader.Read())
			{
				info = new DistributionParamInfo
				{
					DistributionId = TypeHelper.ObjectToInt(reader["distributionid"]),
					ProductPumpedType = (DistributionProductPumpedType)TypeHelper.ObjectToInt(reader["productpumpedtype"]),
					ProductPumpedValue = TypeHelper.ObjectToDecimal(reader["productpumpedvalue"]),
					TotalLevel = TypeHelper.ObjectToInt(reader["totallevel"]),
					LevelPercent = new List<Tuple<int, double>>()
				};
				foreach (var item in reader["levelpercent"].ToString().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
				{
					info.LevelPercent.Add(new Tuple<int, double>(TypeHelper.StringToInt(item.Split(':').First()), double.Parse(item.Split(':').Last())));
				}
				reader.Close();
			}
			return info;
		}
		/// <summary>
		/// 保存分销参数
		/// </summary>
		/// <param name="info"></param>
		public static void SaveDistributionParam(DistributionParamInfo info)
		{
			BMAData.RDBS.SaveDistributionParam(info);
		}

		#endregion

		#region 分销商品设置

		public static List<DistributionProductSetListInfo> GetDistributionProductSetList(int pageSize, int pageNumber, string condition,
			string sort, out long total)
		{
			var list = new List<DistributionProductSetListInfo>();
			var dt = BMAData.RDBS.GetDistributionProductSetList(pageSize, pageNumber, condition, sort, out total);
			if (dt == null || dt.Rows.Count == 0)
			{
				return list;
			}
			list.AddRange(from DataRow row in dt.Rows
				select new DistributionProductSetListInfo
				{
					Id = TypeHelper.ObjectToInt(row["id"], 0),
					ProductId = TypeHelper.ObjectToInt(row["productid"]),
					PumpedType = (DistributionProductPumpedType) TypeHelper.ObjectToInt(row["pumpedtype"], 0),
					PumpedValue = TypeHelper.ObjectToDecimal(row["pumpedvalue"], 0),
					ProductName = row["productname"] as string,
					CostPrice = TypeHelper.ObjectToDecimal(row["costprice"]),
					ShopPrice = TypeHelper.ObjectToDecimal(row["shopprice"])
				});
			return list;
		}

		/// <summary>
		/// 保存分销商品设置信息
		/// </summary>
		/// <param name="info"></param>
		public static void SaveDistributionProductSet(DistributionProductSetInfo info)
		{
			BMAData.RDBS.SaveDistributionProductSet(info);
		}

		/// <summary>
		/// 删除分销商品设置信息
		/// </summary>
		/// <param name="id"></param>
		public static void DeleteDistributionProductSet(int id)
		{
			BMAData.RDBS.DeleteDistributionProductSet(id);
		}

		#endregion

		#region 分销商管理

		public static List<DistributorListInfo> GetDistributorList(int pageSize, int pageNumber, string condition,
			string sort, out long total)
		{
			var tb= BMAData.RDBS.GetDistributorList(pageSize, pageNumber, condition, sort, out total);
			var list = new List<DistributorListInfo>();

			foreach (DataRow row in tb.Rows)
			{
				list.Add(new DistributorListInfo
				{
					Id=TypeHelper.ObjectToInt(row["id"]),
					UserId = TypeHelper.ObjectToInt(row["userid"]),
					Code=row["code"] as string,
					JoinTime = TypeHelper.ObjectToDateTime(row["jointime"])
				});
			}

			return list;
		}

		#endregion
	}
}
